package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lsx
 * @since 2020-08-01
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //根据课程id查询所有的章节Chapter  QueryWrapper中的泛型是数据库表对应的实体类
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(queryWrapper);

        //根据课程id 获取所有的小结
        QueryWrapper<EduVideo> queryWrapperVideo = new QueryWrapper();
        queryWrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(queryWrapperVideo);

        List<ChapterVo> finalList = new ArrayList<>();

        for (EduChapter eduChapter : eduChapters) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            List<VideoVo> videoList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideos) {
                if(eduVideo.getChapterId().equals(eduChapter.getId())){

                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoList.add(videoVo);

                }
            }
            chapterVo.setChildren(videoList);
            finalList.add(chapterVo);
        }


        return finalList;
    }

    /**
     * 根据章节id 删除章节  如果章节下有小节 则不删除
     * @param id
     * @return 返回是否删除成功对象
     */
    @Override
    public boolean deleteById(String id) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",id);
        int count = eduVideoService.count(queryWrapper);
        if(count>0){
            //count >0 说明章节下面有小节
            throw  new GuliException(20001,"不能删除章节，因为章节中有小节");
        }else{
            int result = baseMapper.deleteById(id);
        return   result>0;
        }
    }
    //2.根据课程id删除章节
    @Override
    public void deleteChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
